package com.woniuxy.yogaapp.configuration;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import com.woniuxy.yogaapp.realm.UserRealm;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;

@Configuration // 将当前类指定为配置类
public class ShiroConfiguration {

	// 配置加密
	@Bean
	public CredentialsMatcher matcher() {
		HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
		matcher.setHashAlgorithmName("MD5");
		matcher.setHashIterations(1024);
		return matcher;
	}

	// 配置realm
	@Bean
	public UserRealm getRealm(CredentialsMatcher matcher) {
		UserRealm realm = new UserRealm();
		// 设置加密类型及次数
		realm.setCredentialsMatcher(matcher);
		// 设置缓存
		// 设置加密类型及次数
		realm.setCredentialsMatcher(matcher);
		// 设置缓存
		realm.setCacheManager(ehCacheManager());
		return realm;
	}

	// 配置安全管理器
	@Bean
	public DefaultWebSecurityManager getManager(UserRealm realm, SessionManager sessionManager) {
		// System.out.println("manager:" + realm);
		DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
		manager.setRealm(realm);
		// 设置缓存
		manager.setCacheManager(ehCacheManager());
		// 设置session
		manager.setSessionManager(sessionManager);
		return manager;

	}

	// 过滤器
	@Bean
	public ShiroFilterFactoryBean getFactoryBean(DefaultWebSecurityManager manager) {
		ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
		bean.setSecurityManager(manager);
		// 登录界面
		bean.setLoginUrl("/login.html");
		// 没有权限
		bean.setUnauthorizedUrl("/html/error.html");
		// 过滤器链
		Map<String, String> map = new LinkedHashMap<>(); // 需要保证有序
		
		// 设置
		map.put("/index.html", "anon");
		map.put("/login.html", "anon");
		map.put("/Club.html", "anon");
		map.put("/register.html", "anon");
		map.put("/findpass.html", "anon");
		map.put("/user/**", "anon");
		map.put("/druid/**", "anon");
		map.put("/logout", "logout");
		map.put("/pocket/**", "anon");
		map.put("/user/getRoles", "anon");
		map.put("/ClubMap.html", "anon");
		map.put("/CoachMap.html", "anon");
		map.put("/map/**", "anon");
		// 静态资源放行
		map.put("/css/**", "anon");
		map.put("/bootstrap/**", "anon");
		map.put("/fonts/**", "anon");
		map.put("/html/**", "anon");
		map.put("/image/**", "anon");
		map.put("/images/**", "anon");
		map.put("/js/**", "anon");
		map.put("/lib/**", "anon");
		map.put("/image/**", "anon");
		map.put("/image/**", "anon");
		// 权限页面
		map.put("/html/error.html", "anon");
		// map.put("/studentInfo.html", "roles[teacher]");
		map.put("/orderShowStudent.html", "roles[student]");
		map.put("/orderShowCoach.html", "roles[coach]");
		map.put("/**", "authc");
		bean.setFilterChainDefinitionMap(map);
		return bean;

	}

	// 注解
	@Bean
	public AuthorizationAttributeSourceAdvisor createAasa(DefaultWebSecurityManager securityManager) {
		AuthorizationAttributeSourceAdvisor aasa = new AuthorizationAttributeSourceAdvisor();
		aasa.setSecurityManager(securityManager);
		return aasa;
	}

	// 注解aop
	@Bean
	@DependsOn("lifecycleBeanPostProcessor")
	@ConditionalOnMissingBean
	public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
		DefaultAdvisorAutoProxyCreator defaultAAP = new DefaultAdvisorAutoProxyCreator();
		defaultAAP.setProxyTargetClass(true);
		return defaultAAP;
	}

	@Bean
	public EhCacheManager ehCacheManager() {
		EhCacheManager cacheManager = new EhCacheManager();
		cacheManager.setCacheManagerConfigFile("classpath:ehcache.xml");
		return cacheManager;
	}

	// 生命周期管理器
	@Bean(name = "lifecycleBeanPostProcessor")
	public LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
		return new LifecycleBeanPostProcessor();
	}

	// 配置session
	@Bean
	public SessionManager getSessionManager() {
		DefaultWebSessionManager dwm = new DefaultWebSessionManager();
		dwm.setGlobalSessionTimeout(600000);
		return dwm;
	}
	@Bean
	public ShiroDialect shiroDialect() {
	return new ShiroDialect();//为了在 thymeleaf 里使用 shiro 的标签的 bean
	}

}
